package com.lwj.liwanjia.view.activity.work.shang_jia.ding_dan.tui_huo_tui_kuan;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.DingAdapter01;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.DingBean02;
import com.lwj.liwanjia.bean.DingBean06;
import com.lwj.liwanjia.config.URLConstant;
import com.lwj.liwanjia.utils.BaseUtils;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.view.activity.work.shang_jia.ding_dan.DingDanXiangQingHomeActivity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by 001 on 2017/9/22.
 */

public class ShenQingXiangQingActivity extends BaseActivity implements View.OnClickListener {

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 0:
                    Toast.makeText(ShenQingXiangQingActivity.this, "数据有误", Toast.LENGTH_LONG).show();
                    break;
                case 1:

                    if (homeList != null && homeList.size() >= 0) {

                        LinearLayoutManager manager = new LinearLayoutManager(ShenQingXiangQingActivity.this);
                        mRecyclerView.setLayoutManager(manager);
                        homeAdapter = new DingAdapter01(ShenQingXiangQingActivity.this, homeList);
                        mRecyclerView.setAdapter(homeAdapter);
                    }

                    if (!TextUtils.isEmpty(dingDanBianHao)) {
                        tv_dingDanHao.setText(dingDanBianHao);
                    }

                    if (!TextUtils.isEmpty(xiaDanShiJian)) {
                        tv_time.setText(xiaDanShiJian);
                    }

                    if (!TextUtils.isEmpty(peiSongFangSHi)) {
                        tv_peiSongFangShi.setText(peiSongFangSHi);
                    }

                    if (!TextUtils.isEmpty(zhiFuFangSHi)) {
                        tv_zhiFuFangShi.setText(zhiFuFangSHi);
                    }

                    break;

                case 2:
                    Toast.makeText(ShenQingXiangQingActivity.this, "提交成功", Toast.LENGTH_LONG).show();
                    finish();
                    break;
                case 5:
                    Toast.makeText(ShenQingXiangQingActivity.this, "余额不足", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    private TextView tv_back;
    private TextView tv_title;
    private LinearLayout ll_huanHuoYuanYin;//换货原因
    private LinearLayout ll_huanHuoShuoMing;//换货说明
    private LinearLayout ll_huoWuZhuangTai;//货物状态
    private LinearLayout ll_tuiKuanYuanYun;//退款原因
    private LinearLayout ll_tuiKuanJingEr;//退款金额
    private TextView tv_hanYunFei;//含运费
    private LinearLayout ll_tuiKuanShuoMing;//退款说明
    private String id;//订单号
    private RecyclerView mRecyclerView;
    private List<DingBean02.DataBean.CommodityBean> homeList;
    private DingAdapter01 homeAdapter;
    private TextView tv_buTongYi;//不同意
    private TextView tv_tongYi;//同意

    private TextView tv_dingDanHao;//订单号
    private TextView tv_time;//时间
    private TextView tv_wuLiuHao;//物流单号
    private TextView tv_peiSongFangShi;//配送方式
    private TextView tv_zhiFuFangShi;//支付方式
    private String dingDanBianHao;
    private String xiaDanShiJian;
    private String peiSongFangSHi;
    private String zhiFuFangSHi;
    private String tuiState;


    @Override
    protected void initView() {

        tv_back = (TextView) findViewById(R.id.tv_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        ll_huanHuoYuanYin = (LinearLayout) findViewById(R.id.ll_huanHuoYuanYin);
        ll_huanHuoShuoMing = (LinearLayout) findViewById(R.id.ll_huanHuoShuoMing);
        ll_huoWuZhuangTai = (LinearLayout) findViewById(R.id.ll_huoWuZhuangTai);
        ll_tuiKuanYuanYun = (LinearLayout) findViewById(R.id.ll_tuiKuanYuanYun);
        ll_tuiKuanJingEr = (LinearLayout) findViewById(R.id.ll_tuiKuanJingEr);
        tv_hanYunFei = (TextView) findViewById(R.id.tv_hanYunFei);
        ll_tuiKuanShuoMing = (LinearLayout) findViewById(R.id.ll_tuiKuanShuoMing);

        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        tv_buTongYi = (TextView) findViewById(R.id.tv_left_bottom);
        tv_tongYi = (TextView) findViewById(R.id.tv_right_bottom);
        tv_dingDanHao = (TextView) findViewById(R.id.tv_dingDanHao);
        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_wuLiuHao = (TextView) findViewById(R.id.tv_wuLiuDanHao);
        tv_peiSongFangShi = (TextView) findViewById(R.id.tv_peiSongFangSHi);
        tv_zhiFuFangShi = (TextView) findViewById(R.id.tv_zhiFuFangShi);


        tv_back.setVisibility(View.VISIBLE);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText("申请退货");

        tv_back.setOnClickListener(this);
        tv_buTongYi.setOnClickListener(this);
        tv_tongYi.setOnClickListener(this);
    }

    @Override
    protected void initData() {

        Intent intent = getIntent();
        tuiState = intent.getStringExtra("tuiState");
        id = intent.getStringExtra("id");


        //根据状态 改变UI
        byStateChangUi(tuiState);

        //请求网络 获取信息
        initOkHttp01();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_shen_qing_xiang_qing;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回
            case R.id.tv_left:
                finish();
                break;
            //不同意
            case R.id.tv_left_bottom:

                //连网 不同意
                initOkHttp04();

                break;
            //同意
            case R.id.tv_right_bottom:

                switch (tuiState) {

                    case "正在申请(待发货)":
                        //连网 同意 待发货
                        initOkHttp02();
                        break;
                    case "正在申请(退款)":
                    case "正在申请(换货)":
                    case "正在申请(退款退货)":
                        initOkHttp03();
                        break;
                }

                break;
        }
    }

    //根据状态 改变UI
    private void byStateChangUi(String tuiState) {

        switch (tuiState) {
            case "退货退款申请":
                ll_tuiKuanYuanYun.setVisibility(View.VISIBLE);
                ll_tuiKuanJingEr.setVisibility(View.VISIBLE);
                tv_hanYunFei.setVisibility(View.VISIBLE);
                ll_tuiKuanShuoMing.setVisibility(View.VISIBLE);
                break;
            case "换货申请":
                ll_huanHuoYuanYin.setVisibility(View.VISIBLE);
                ll_huanHuoShuoMing.setVisibility(View.VISIBLE);
                break;
            case "退款申请":
                ll_huoWuZhuangTai.setVisibility(View.VISIBLE);
                ll_tuiKuanYuanYun.setVisibility(View.VISIBLE);
                ll_tuiKuanJingEr.setVisibility(View.VISIBLE);
                tv_hanYunFei.setVisibility(View.VISIBLE);
                ll_tuiKuanShuoMing.setVisibility(View.VISIBLE);
                break;
        }

        tv_title.setText(tuiState);
    }

    //请求网络 获取信息
    private void initOkHttp01() {

        String url = URLConstant.BASE_URL + "order/shopOneOrder";
        String token = SpfUtils.getSpfUtils(this).getToken();

        OkHttpClient mOkHttpClient = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("tradeId", id)
                .add("token", token)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        Call mCall = mOkHttpClient.newCall(request);
        mCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                try {
                    String result = response.body().string();
                    parse01(result);
                } catch (Exception e) {

                }
            }
        });
    }

    //解析
    private void parse01(String result) {

        Gson gson = new Gson();
        DingBean02 bean = gson.fromJson(result, DingBean02.class);

        if (bean != null) {

            switch (bean.getState()) {

                case 0:
                    mHandler.sendEmptyMessage(0);
                    break;

                case 1:

                    homeList = new ArrayList<>();

                    for (int i = 0; i < bean.getData().getCommodity().size(); i++) {

                        DingBean02.DataBean.CommodityBean commodityBean = bean.getData().getCommodity().get(i);
                        homeList.add(commodityBean);
                    }

                    dingDanBianHao = bean.getData().getTradeId();

                    long time = bean.getData().getOrderTime();
                    //下单时间
                    xiaDanShiJian = BaseUtils.getString(String.valueOf(time / 1000));

                    zhiFuFangSHi = bean.getData().getPayWay();

                    //物流单号 配送方式
                    peiSongFangSHi = bean.getData().getLogistics().getLogisticsType()+"";
                    mHandler.sendEmptyMessage(1);

                    break;
            }
        }
    }

    //连网 同意 待发货
    private void initOkHttp02() {

        String url = URLConstant.BASE_URL + "oneCard/pay/returnSend";
        String token = SpfUtils.getSpfUtils(this).getToken();

        OkHttpClient mOkHttpClient = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("tradeId", id)
                .add("token", token)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        Call mCall = mOkHttpClient.newCall(request);
        mCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                try {
                    String result = response.body().string();
                    parse02(result);
                } catch (Exception e) {

                }
            }
        });
    }

    //解析 同意 待发货
    private void parse02(String result) {

        Gson gson = new Gson();
        DingBean06 bean = gson.fromJson(result, DingBean06.class);

        if (bean != null) {

            switch (bean.getState()) {

                case 0:
                    mHandler.sendEmptyMessage(0);
                    break;

                case 1:
                    mHandler.sendEmptyMessage(2);
                    break;
            }
        }

    }

    //连网 同意 已完成
    private void initOkHttp03() {

        String url = URLConstant.BASE_URL + "oneCard/pay/returnEnd";
        String token = SpfUtils.getSpfUtils(this).getToken();

        OkHttpClient mOkHttpClient = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("tradeId", id)
                .add("token", token)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        Call mCall = mOkHttpClient.newCall(request);
        mCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                try {
                    String result = response.body().string();
                    parse03(result);
                } catch (Exception e) {

                }
            }
        });
    }

    //解析 同意 已完成
    private void parse03(String result) {

        Gson gson = new Gson();
        DingBean06 bean = gson.fromJson(result, DingBean06.class);

        if (bean != null) {

            switch (bean.getState()) {

                case 0:
                    mHandler.sendEmptyMessage(5);//退款失败（余额不足）
                    break;

                case 1:
                    mHandler.sendEmptyMessage(2);
                    break;
            }
        }

    }

    //连网 不同意
    private void initOkHttp04() {

        String url = URLConstant.BASE_URL + "oneCard/pay/noReturn";
        String token = SpfUtils.getSpfUtils(this).getToken();

        OkHttpClient mOkHttpClient = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("tradeId", id)
                .add("token", token)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        Call mCall = mOkHttpClient.newCall(request);
        mCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    String result = response.body().string();
                    parse04(result);
                } catch (Exception e) {

                }
            }
        });
    }

    //解析  不同意
    private void parse04(String result) {

        Gson gson = new Gson();
        DingBean06 bean = gson.fromJson(result, DingBean06.class);

        if (bean != null) {

            switch (bean.getState()) {

                case 0:
                    mHandler.sendEmptyMessage(0);
                    break;

                case 1:
                    mHandler.sendEmptyMessage(2);
                    break;
            }
        }
    }
}
